package com.kehao.leetcode.middle.other;

import org.junit.Test;

public class MajorityElementSolution {
    public int majorityElement(int[] nums) {
        int candidate = 0,count = 0;
        for (int i = 0; i < nums.length; i++) {
            if(count==0){
                candidate = nums[i];
            }
            if(candidate==nums[i]){
                count++;
            }else {
                count --;
            }
        }
        return candidate;
    }

    @Test
    public void test01(){
        System.out.println(majorityElement(new int[]{2, 2, 1, 1, 1, 2, 2}));
    }
}
